var navwidth = snavwidth = mnavwidth = tnavwidth = ismobile = '';
navwidth = '230px';
snavwidth = '60px';
mnavwidth = '0px';
tnavwidth = 300;
ismobile = goPAGE();
layui.use(['element', 'layer'], function () {
    var element = layui.element, layer = layui.layer;
    element.on('nav(layadmin-layout-left)', function (elem) {
        var event = elem[0].getAttribute('layadmin-event');
        switch (event) {
            case'flexible':
                $('#LAY_app_flexible').toggleClass('layui-icon-spread-left');
                $('#LAY_app_flexible').toggleClass('layui-icon-spread-right');
                if (ismobile) {
                    $('#LAY_app_body').width(document.body.clientWidth + 'px');
                    if ($('#LAY_app_flexible').hasClass('layui-icon-spread-left')) {
                        $('#LAY_app').animate({'width': mnavwidth}, tnavwidth);
                        $('#LAY_WRAP .layui-body').animate({'left': mnavwidth}, tnavwidth);
                        $('#mobilenav').removeClass('mobilenav')
                    } else {
                        $('#LAY_app').animate({'width': navwidth}, tnavwidth);
                        $('#LAY_WRAP .layui-body').animate({'left': navwidth}, tnavwidth);
                        $('#LAY_app').removeClass('layadmin-side-shrink');
                        $('#mobilenav').addClass('mobilenav')
                    }
                } else {
                    if ($('#LAY_app_flexible').hasClass('layui-icon-spread-left')) {
                        $('#LAY_app').animate({'width': snavwidth}, tnavwidth);
                        $('#LAY_WRAP .layui-body').animate({'left': snavwidth}, tnavwidth);
                        $('#LAY_app').addClass('layadmin-side-shrink')
                        //改变右侧变大
                        $('#LAY_WRAP').addClass("LAY_WRAP_BIG")
                        $('#LAY_WRAP').removeClass("LAY_WRAP")
                    } else {
                        $('#LAY_app').animate({'width': navwidth}, tnavwidth);
                        $('#LAY_WRAP .layui-body').animate({'left': navwidth}, tnavwidth);
                        $('#LAY_app').removeClass('layadmin-side-shrink')
                        //改变右侧变小
                        $('#LAY_WRAP').removeClass("LAY_WRAP_BIG")
                        $('#LAY_WRAP').addClass("LAY_WRAP")
                    }
                }
                break
        }
    });
    element.on('nav(layadmin-layout-right)', function (elem) {
        var event = elem[0].getAttribute('layadmin-event');
        switch (event) {
            case'theme':
                $('#setbgcolor').css('display', 'block');
                break
        }
    });
    $(".layui-nav-left li").mouseover(function () {
        if ($('#LAY_app_flexible').hasClass('layui-icon-spread-left')) {
            var that = this;
            layer.tips($(this).find('cite').html(), that, {time: 0})
        }
    });
    $(".layui-nav-left li").mouseleave(function () {
        if ($('#LAY_app_flexible').hasClass('layui-icon-spread-left')) {
            layer.closeAll('tips')
        }
    });
    element.on('nav(layui-nav-left)', function (elem) {
        if ($('#LAY_app_flexible').hasClass('layui-icon-spread-left')) {
            layer.closeAll('tips');
            $('#LAY_app_flexible').removeClass('layui-icon-spread-left');
            $('#LAY_app_flexible').addClass('layui-icon-spread-right');
            $('#LAY_app').animate({'width': navwidth}, tnavwidth);
            $('#LAY_WRAP .layui-body').animate({'left': navwidth}, tnavwidth);
            $('#LAY_app').removeClass('layadmin-side-shrink')
        }
    })
});
if (ismobile) {
    $('#mobilenav').removeClass('mobilenav');
    $('#LAY_app_body').width(document.body.clientWidth + 'px');
    if ($('#LAY_app_flexible').hasClass('layui-icon-shrink-right')) {
        $('#LAY_app_flexible').removeClass('layui-icon-spread-right');
        $('#LAY_app_flexible').addClass('layui-icon-spread-left');
        $('#LAY_app').attr('style', 'width:' + mnavwidth);
        $('#LAY_WRAP .layui-body').attr('style', 'left:' + mnavwidth);
        $('#LAY_app').removeClass('layadmin-side-shrink');
        $('#mobilenav').removeClass('mobilenav')
    }
}
window.onresize = function () {
    ismobile = goPAGE();
    $('#mobilenav').removeClass('mobilenav');
    if (ismobile) {
        $('#LAY_app_body').width(document.body.clientWidth + 'px');
        if ($('#LAY_app_flexible').hasClass('layui-icon-shrink-right')) {
            $('#LAY_app_flexible').removeClass('layui-icon-spread-right');
            $('#LAY_app_flexible').addClass('layui-icon-spread-left');
            $('#LAY_app').animate({'width': mnavwidth}, tnavwidth);
            $('#LAY_WRAP .layui-body').animate({'left': mnavwidth}, tnavwidth);
            $('#LAY_app').removeClass('layadmin-side-shrink');
            $('#mobilenav').removeClass('mobilenav')
        }
    } else {
        $('#LAY_app_body').removeAttr('style');
        if ($('#LAY_app_flexible').hasClass('layui-icon-shrink-right')) {
            $('#LAY_app_flexible').removeClass('layui-icon-spread-left');
            $('#LAY_app_flexible').addClass('layui-icon-spread-right');
            $('#LAY_app').animate({'width': navwidth}, tnavwidth);
            $('#LAY_WRAP .layui-body').animate({'left': navwidth}, tnavwidth);
            $('#LAY_app').removeClass('layadmin-side-shrink')
        }
    }
};
$('#setbgcolorshade').click(function () {
    $('#setbgcolor').css('display', 'none')
});

function clearmobilenav() {
    $('#mobilenav').removeClass('mobilenav');
    if ($('#LAY_app_flexible').hasClass('layui-icon-shrink-right')) {
        $('#LAY_app_flexible').removeClass('layui-icon-spread-right');
        $('#LAY_app_flexible').addClass('layui-icon-spread-left');
        $('#LAY_app').animate({'width': mnavwidth}, tnavwidth);
        $('#LAY_WRAP .layui-body').animate({'left': mnavwidth}, tnavwidth);
        $('#LAY_app').removeClass('layadmin-side-shrink');
        $('#mobilenav').removeClass('mobilenav')
    }
}

function goPAGE() {
    if ((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))) {
        return true
    }
    return false
}

$('#ChangeColor li').click(function () {
    $('#ChangeColor li').removeClass('layui-this');
    $(this).toggleClass('layui-this');
    var colorleve = parseInt($(this).attr('data-index'));
    setCookie('colorleve', colorleve);
    setcolorleve(colorleve)
});
if (getCookie("colorleve") != '') {
    var colorleve = (getCookie("colorleve"));
    $.each($('#ChangeColor li'), function (v, k) {
        if (v == colorleve) {
            k.setAttribute('class', 'layui-this')
        }
    });
    setcolorleve(colorleve)
} else {
    $('#ChangeColor li').eq(8).addClass('layui-this');
    setcolorleve(8)
}

function setcolorleve(colorleve) {
    colorleve = parseInt(colorleve);
    var navleft = nvatop = navright = nvarightfont = navleftclick = chancolor = '';
    navright = '#fff';
    nvarightfont = '#333';
    switch (colorleve) {
        case 0:
            navleftclick = '#009688';
            navleft = '#20222A';
            nvatop = '#20222A';
            break;
        case 1:
            navleftclick = '#3B91FF';
            navleft = '#03152A';
            nvatop = '#03152A';
            break;
        case 2:
            navleftclick = '#A48566';
            navleft = '#2E241B';
            nvatop = '#2E241B';
            break;
        case 3:
            navleftclick = '#7A4D7B';
            navleft = '#50314F';
            nvatop = '#50314F';
            break;
        case 4:
            navleftclick = '#1E9FFF';
            navleft = '#344058';
            nvatop = '#1E9FFF';
            break;
        case 5:
            navleftclick = '#5FB878';
            navleft = '#3A3D49';
            nvatop = '#2F9688';
            break;
        case 6:
            navleftclick = '#F78400';
            navleft = '#20222A';
            nvatop = '#F78400';
            break;
        case 7:
            navleftclick = '#AA3130';
            navleft = '#28333E';
            nvatop = '#AA3130';
            break;
        case 8:
            navleftclick = '#009688';
            navleft = '#24262F';
            nvatop = '#3A3D49';
            break;
        case 9:
            navleftclick = '#009688';
            navleft = '#20222A';
            nvatop = '#226A62';
            navright = '#2F9688';
            nvarightfont = '#f8f8f8';
            break;
        case 10:
            navleftclick = '#1E9FFF';
            navleft = '#344058';
            nvatop = '#0085E8';
            navright = '#1E9FFF';
            nvarightfont = '#f8f8f8';
            break;
        case 11:
            navleftclick = '#009688';
            navleft = '#20222A';
            nvatop = '#20222A';
            navright = '#393D49';
            nvarightfont = '#f8f8f8';
            break;
        case 12:
            navleftclick = '#7A4D7B';
            navleft = '#50314F';
            nvatop = '#50314F';
            navright = '#50314F';
            nvarightfont = '#f8f8f8';
            break;
        case 13:
            navleftclick = '#AA3130';
            navleft = '#28333E';
            nvatop = '#28333E';
            navright = '#AA3130';
            nvarightfont = '#f8f8f8';
            break;
        case 14:
            navleftclick = '#009688';
            navleft = '#28333E';
            nvatop = '#009688';
            navright = '#009688';
            nvarightfont = '#f8f8f8';
            break
    }
    chancolor = '.layui-side-scroll,.layui-bg-black,.layui-nav-left{background-color:' + navleft + ' !important;}.layui-side-scroll .layui-logo{background-color:' + nvatop + ' !important;}.layui-header{background-color:' + navright + ' !important;}.layui-body .layui-header a, .layui-body .layui-header a cite{color:' + nvarightfont + ' !important;}.layui-header .layui-nav .layui-nav-more{border-top-color:' + nvarightfont + ' !important;}.layui-nav-tree .layui-nav-child dd.layui-this, .layui-nav-tree .layui-nav-child dd.layui-this a, .layui-nav-tree .layui-this, .layui-nav-tree .layui-this>a, .layui-nav-tree .layui-this>a:hover{background-color: ' + navleftclick + '; !important;}.layui-body .layui-header .layui-layout-right .layui-nav-child a{color:#333 !important;}';
    $('#LAY_layadmin_theme').html(chancolor)
}

function setCookie(name, value, hours) {
    var d = new Date();
    d.setTime(d.getTime() + hours * 3600 * 1000);
    document.cookie = name + '=' + value + '; expires=' + d.toGMTString()
}

function getCookie(name) {
    var arr = document.cookie.split('; ');
    for (var i = 0; i < arr.length; i++) {
        var temp = arr[i].split('=');
        if (temp[0] == name) {
            return temp[1]
        }
    }
    return ''
}

function removeCookie(name) {
    var d = new Date();
    d.setTime(d.getTime() - 10000);
    document.cookie = name + '=1; expires=' + d.toGMTString()
}

/**
 * 路由生成
 * @param routeUrl route_uri(路由名称)
 * @param param 路由参数{a:1,b:2,c:3,......}
 * */
function route (routeUrl, param) {
    let append = [];
    for (let x in param) {
        let search = '{' + x + '}';
        if (routeUrl.indexOf(search) >= 0) {
            routeUrl = routeUrl.replace('{' + x + '}', param[x]);
        } else {
            append.push(x + '=' + param[x]);
        }
    }
    let url = '/' + _.trimStart(routeUrl, '/');
    if (append.length == 0) {
        return url;
    }
    if (url.indexOf('?') >= 0) {
        url += '&';
    } else {
        url += '?';
    }
    url += append.join('&');
    return url;
}

/**
 * 表格下拉菜单
 * @param that 当前元素的this指向
 * @param buttons 操作的按钮组元素，格式
 * [{title: '查看详情', id: '按钮唯一主键',type:'link',link:"{{ route_uri(路由名称)}}",params:路由参数},//链接格式
 *  {title: '修改密码', id: '按钮唯一主键',type:"openLayerForm",link:"{{route_uri(路由名称)}}",params:路由参数,set:{title:"弹窗名称",method:"路由申请模式GET/POST",width:"弹窗宽",height:"弹窗高",repush:是否刷新,dome:"弹窗的id"}},//弹窗格式
 *  {title: '删除角色', id: '按钮唯一主键',type:'tableDataDelete',link:"{{ route_uri(路由名称)}}",params:路由参数,set:{dome:this,title:"删除提示信息"}},//删除格式
 *  ]
 * @param buttons中type说明 link:链接 openLayerForm:弹窗 tableDataDelete:删除
 *        buttons中params说明 路由参数 格式为{id:id,name:name,...}
 *        buttons中set说明， 设置弹窗及删除模式需要的参数，set在弹窗及删除模式下生效，
 * @param extParams 下拉菜单扩展项，可补充一写下拉菜单需要其他属性，格式为{attr1:one,attr2:two,...}具体内容参考layui文档下拉菜单参数说明
 * */
function tableButtonGroup(that,buttons,extParams={}){
    let base = {
        elem: that
        , show: true //外部事件触发即显示
        , data: buttons
        , align: 'right' //右对齐弹出（v2.6.8 新增）
        , style: 'box-shadow: 1px 1px 10px rgb(0 0 0 / 12%);' //设置额外样式
        , click: function (data, othis) {
            const type = data.type;
            if (type == "link") {
                location.href = route(data.link, data.params)
                return true;
            }
            if (type == "openLayerForm") {
                admin.openLayerForm(route(data.link, data.params), data.set.title, data.set.method, data.set.width, data.set.height, data.set.repush, data.set.dome)
                return true;
            }
            if(type == "tableDataDelete"){
                admin.tableDataDelete(route(data.link, data.params), data.set.dome, data.set.title)
            }
        }
    }

    return Object.assign(base, extParams);
}
